<template>
  <div class="skuitem">
    <div v-if="search_visible" id="search" class="search">
      <el-form ref="form" :model="searchData">
        <el-row :gutter="10">
          <el-col :span="5">
            <el-form-item>
              <el-input
                id="company_name"
                v-model="searchData.company_name"
                placeholder="请输入公司简称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <el-input
                id="company_name"
                v-model="searchData.product_title"
                placeholder="请输入商品名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <search-tool
                style="text-align: right"
                :search_data.sync="searchData"
                @refesh_list="getSkuItemList"
              ></search-tool>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="sku_header_left">
      <div>
        <el-button
          type="primary"
          size="small"
          class="el-icon-add"
          @click="addSkuList"
        >
          新增SKU组
        </el-button>
      </div>
      <TableTool
        :search_visible.sync="search_visible"
        @refesh_list="getSkuItemList"
      ></TableTool>
    </div>
    <el-table
      :height="
        search_visible
          ? $store.state.user.clientHeight - 280
          : $store.state.user.clientHeight - 240
      "
      :data="tableData"
      :span-method="reconstructionStuCell"
      style="width: 100%"
      :header-cell-style="{
        background: '#f5f7fa',
        color: '#14142B',
        height: '28px',
        padding: '5px 0',
      }"
      :cell-style="{
        color: '#606266',
        fontSize: '13px',
        padding: '7px 0',
      }"
      v-loading="loading"
      :row-class-name="tableRowClassName"
      :border="false"
    >
      <template slot="empty">
        <img
          class="empty-img"
          src="@/assets/imgs/zanwushuju.svg"
          style="width: 64px; height: 50px"
          alt=""
        />
        <div class="noCart">暂无数据</div>
      </template>
      <el-table-column align="center" prop="Serial_No" width="80" label="序号">
      </el-table-column>
      <el-table-column align="left" prop="" label="品名">
        <template slot-scope="scope">
          <div v-if="scope.row.sku_item_No" class="labels">
            <span>SKU组号：</span>{{ scope.row.sku_item_No }}
          </div>
          <div v-if="scope.row.product_item_title" class="labels">
            <span>SKU组名：</span>{{ scope.row.product_item_title.zh }}
          </div>
          <div v-if="scope.row.item_univalent" class="labels">
            <span>总价值：</span>{{ scope.row.item_univalent }}&nbsp;<span
              style="font-weight: bold; color: #0cc50c"
              >USD</span
            >
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="SKU信息">
        <el-table-column align="left" label="品名">
          <template slot-scope="scope">
            <div v-if="scope.row.product_title.zh" class="labels">
              <span>中文品名：</span>{{ scope.row.product_title.zh }}
            </div>
            <div v-if="scope.row.product_title.en" class="labels">
              <span>英文品名：</span>{{ scope.row.product_title.en }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="sku_No"
          label="本地SKU"
        ></el-table-column>
        <el-table-column align="center" label="商品属性">
          <template slot-scope="scope">
            <div v-if="scope.row.specifications">
              <el-checkbox
                v-model="scope.row.specifications.charged"
                class="checkbtn labels"
                ><span class="labels">是否带电?</span>
              </el-checkbox>
            </div>
            <div v-if="scope.row.specifications">
              <el-checkbox
                v-model="scope.row.specifications.liquid"
                class="checkbtn labels"
                ><span class="labels">是否液体?</span>
              </el-checkbox>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="单价">
          <template slot-scope="scope">
            <div v-if="scope.row.univalent" class="labels">
              <span>单价：</span>{{ scope.row.univalent }}&nbsp;<span
                style="font-weight: bold; color: #0cc50c"
                >USD</span
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="amount"
          width="120"
          label="数量"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="inventory"
          width="120"
          label="库存"
        ></el-table-column>
        <div>合计</div>
      </el-table-column>
      <el-table-column align="center" label="总计">
        <el-table-column
          align="center"
          prop="allAmount"
          width="150"
          label="总数"
        >
        </el-table-column>
        <el-table-column
          align="center"
          width="160"
          prop="create_time"
          label="创建时间"
        >
        </el-table-column>
        <el-table-column align="center" width="150" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >修改
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <Pagination :child-msg="pageparm" @callFather="callFather"></Pagination>
    <skuitem-modal
      @submit="submitClick"
      @closeForm="closeDialog"
      :dialogVisible.sync="dialogVisible"
      :editData="editData"
      :dialogTitle="dialogTitle"
    ></skuitem-modal>
  </div>
</template>

<script>
import { reconstructionStuData } from "./config/config";
import {
  getSkuItemList,
  addSkuItem,
  delSkuItem,
  getUpdateInfo,
  updateSkuItem,
} from "@/api/coupang";
import skuitemModal from "./components/skuitemModal.vue";

export default {
  components: {
    skuitemModal,
  },
  data() {
    return {
      tableData: [],
      searchData: {},
      search_visible: true,
      dialogVisible: false,
      dialogTitle: "",
      editData: {},
      pageparm: {
        currentPage: 1,
        pageSize: 20,
        total: 0,
      },
      //---------------------------------------
      loading: false,
      formInline: {
        page: 1,
        limit: 20,
      },
    };
  },
  created() {
    this.getSkuItemList();
  },
  methods: {
    CellStyle: function ({ row, column, rowIndex, columnIndex }) {},
    tableRowClassName: function (row, index) {
      if (row.row.bgColor === true) {
        return "bgColor";
      } else {
      }
    },
    expandSku(row, col) {
      console.log(row);
    },
    submitClick(val) {
      if (val.product_sku_item_id) {
        this.updateSkuItem(val);
      } else {
        this.addSkuItem(val);
      }
    },
    async handleEdit(row) {
      const res = await getUpdateInfo({
        product_sku_item_id: row.product_sku_item_id,
      });
      if (res.data.status === 200) {
        this.dialogVisible = true;
        this.dialogTitle = "修改";
        this.editData = res.data.data;
        this.editData["product_sku_item_id"] = row.product_sku_item_id;
      }
    },
    addSkuList() {
      this.dialogTitle = "新增";
      this.dialogVisible = true;
    },
    // 删除SKU组
    async handleDelete(row) {
      this.$confirm("确定操作吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await delSkuItem({
            product_sku_item_id: row.product_sku_item_id,
          });
          if (res.data.status === 200) {
            this.getSkuItemList();
            this.$message({
              type: "success",
              message: res.data.msg,
            });
          } else {
            this.$message({
              type: "error",
              message: res.data.msg,
            });
          }
        })
        .catch(() => {});
    },
    /**
     * 合并单元格数据
     */
    reconstructionStuCell({ row, column, rowIndex, columnIndex }) {
      let column0Data = row["columnIndex0"];
      let column1Data = row["columnIndex1"];

      let column8Data = row["columnIndex8"];
      let column9Data = row["columnIndex9"];
      let column10Data = row["columnIndex10"];
      //判断条件满足情况下，返回对应的rowspan和colspan数据
      if (
        (column0Data && column0Data.columnIndex == columnIndex) ||
        (column1Data && column1Data.columnIndex == columnIndex) ||
        (column8Data && column8Data.columnIndex == columnIndex) ||
        (column9Data && column9Data.columnIndex == columnIndex) ||
        (column10Data && column10Data.columnIndex == columnIndex)
      ) {
        return {
          rowspan: column1Data.rowspan,
          colspan: column1Data.colspan,
        };
      }
      //if end
    },
    //end
    async getSkuItemList() {
      this.loading = false;
      try {
        let obj = {};
        Object.assign(obj, this.searchData, this.formInline);
        const res = await getSkuItemList(obj);
        if (res.data.status === 200) {
          this.pageparm.currentPage = res.data.data.current_page;
          this.pageparm.total = res.data.data.total;
          this.pageparm.pageSize = res.data.data.per_page;
          res.data.data.data.forEach((item, index) => {
            item.sku_item.forEach((sub_item) => {
              if (index % 2 === 1) {
                sub_item["bgColor"] = true;
              } else {
                sub_item["bgColor"] = false;
              }
            });
          });
          this.tableData = reconstructionStuData(res.data.data.data);
        } else {
          this.$message.error(res.data.msg);
        }
      } catch (error) {
        this.loading = false;
        this.$message.error("系统错误");
      }
    },
    //分页插件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      this.getSkuItemList(this.formInline);
    },
    async addSkuItem(data) {
      const res = await addSkuItem(data);
      if (res.data.status === 200) {
        this.$message({
          type: "success",
          message: res.data.msg,
        });
        this.getSkuItemList();
      }
    },
    //修改提交
    async updateSkuItem(data) {
      const res = await updateSkuItem(data);
      if (res.data.status === 200) {
        this.$message({
          type: "success",
          message: res.data.msg,
        });
        this.getSkuItemList();
      }
    },
    //关闭弹窗
    closeDialog() {
      this.dialogVisible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-table .hidden-row {
  //display: none;
  //visibility: hidden;
}

::v-deep .el-table .bgColor {
  background-color: #f5f7fa;
}

.skuitem {
  padding: 0 10px;
}

.labels {
  font-size: 12px;

  span {
    font-size: 12px;
    color: #a9a9a9;
    font-weight: 400;
  }

  &:hover {
    color: #000;
  }
}

.checkbtn {
  ::v-deep .el-checkbox__inner {
    border-radius: 50%;
  }
}

::v-deep .el-input__inner {
  height: 32px;
  line-height: 32px;
}

.el-form-item {
  margin-bottom: 0;
}

.sku_header_left {
  display: flex;
  justify-content: space-between;
  margin: 10px 0;
}

::v-deep .is-group th {
  text-align: center;
}
</style>
